package com.desmart.renjiao.membersearch;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.MemberInfo;
import com.desmart.renjiao.orgainzation.MemberItemAdapter;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.utils.PinyUtils;
import com.lemon.library.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Route(path = ARoutePath.addresslist.MemberSearchActivity)
public class MemberSearchActivity extends CommonActivity<MemberSearchM,MemberSearchP> implements MemberSearch.MemberSearchV {

    private EditText etSearch;
    private List<MemberInfo> mList = new ArrayList<>();
    private RecyclerView recyclerview;
    private MemberItemAdapter mAdapter;

    @Override
    public int bindLayout() {
        return R.layout.activity_member_search;
    }

    @Override
    public void initView(Activity activity) {
        setTitleText("搜索");
        etSearch = activity.findViewById(R.id.et_search);
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    String word = etSearch.getText().toString();
                    if (TextUtils.isEmpty(word)){
                        ToastUtils.show("请输入搜索内容");
                    }else{
                        mList.clear();
                        mAdapter.notifyDataSetChanged();
                        mPresenter.memberSearch(word);
                    }
                }
                return false;
            }
        });

        recyclerview = activity.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MemberItemAdapter(mList);
        recyclerview.setAdapter(mAdapter);
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void onNetStart(String tag, String startMsg) {
        super.onNetStart(tag, startMsg);
        showLoading();
    }

    @Override
    public void onMemberList(List<MemberInfo> users) {

        Collections.sort(users, new Comparator<MemberInfo>() {
            @Override
            public int compare(MemberInfo user1, MemberInfo user2) {
                String name1 = user1.getUsername();
                String name2 = user2.getUsername();
                int flag = PinyUtils.getAlpha(name1).compareTo(PinyUtils.getAlpha(name2));
                return flag;
            }
        });

        mList.clear();
        mList.addAll(users);
        mAdapter.notifyDataSetChanged();
    }
}
